﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace ImageResizeForAndroid {
    /// <summary>
    /// SettingsWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class SettingsWindow : Window {

        string destFolderName = "";

        // MessageBox用変数
        MessageBoxButton button;
        MessageBoxImage icon;
        string messageBoxText;
        string caption;
        Window owner;

        public SettingsWindow() {
            InitializeComponent();
            currentSettingFolder.Text = Properties.Settings.Default.outputFolder;
            owner = this;
        }

        private void saveButton_Click(object sender, RoutedEventArgs e) {
            messageBoxText = "Would you change the default output folder?";
            button = MessageBoxButton.YesNo;
            icon = MessageBoxImage.Question;
            MessageBoxResult mdr = System.Windows.MessageBox.Show(owner, messageBoxText, caption, button, icon);
            if (mdr == MessageBoxResult.Yes) {
                Properties.Settings.Default.outputFolder = outputFolder.Text;
                Properties.Settings.Default.Save();
                currentSettingFolder.Text = outputFolder.Text;
                this.Close();
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e) {
            this.Close();
        }

        private void chooseFolderButton_Click(object sender, RoutedEventArgs e) {
            outputFolder.Text = OpenDestFolder();
        }

        // フォルダ選択画面を表示
        private String OpenDestFolder() {
            FolderBrowserDialog fdlg = new FolderBrowserDialog();
            fdlg.Description = "Choose destination folder";

            DialogResult dr = fdlg.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK) {
                destFolderName = fdlg.SelectedPath;
            }
            return destFolderName;
        }

        // 出力先フォルダのドラッグオーバー処理
        private void outputFolder_PreviewDragOver(object sender, System.Windows.DragEventArgs e) {
            if (e.Data.GetData(System.Windows.DataFormats.FileDrop) != null) {
                e.Effects = System.Windows.DragDropEffects.Copy;
            } else {
                e.Effects = System.Windows.DragDropEffects.None;
            }
            e.Handled = true;
        }

        // 出力先フォルダのドロップ処理
        private void outputFolder_Drop(object sender, System.Windows.DragEventArgs e) {
            string[] files = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[];
            if (files != null) {
                //最初のファイルのPathを表示
                if (outputFolderExist(files[0])) {
                    destFolderName = files[0];
                }
            }
        }

        private bool outputFolderExist(string destinationPath) {
            if (Directory.Exists(destinationPath)) {
                outputFolder.Text = destinationPath;
                return true;
            } else {
                messageBoxText = "Choose folder not exist or valid.\nPlease choose destination folder, again.";
                caption = "Error";
                button = MessageBoxButton.OK;
                icon = MessageBoxImage.Warning;
                System.Windows.MessageBox.Show(owner, messageBoxText, caption, button, icon);
                return false;
            }
        }
    }
}
